package com.xiao.community.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class DownFile {

    private static String endpoint = "oss-cn-shanghai.aliyuncs.com";
    private static String accessKeyId = "LTAI5tQVprF9BspQGzLoJRtr";
    private static String accessKeySecret = "EIP2XpxgllsVdZnpXM9FuJpD3X6Dy9";
    private static String bucketName = "demobucker";

    public static void DownFiles(String urlimg) throws IOException {


// 填写不包含Bucket名称在内的Object完整路径，例如testfolder/exampleobject.txt。
        String  utls = urlimg;
        String[] split = utls.split("https://demobucker.oss-cn-shanghai.aliyuncs.com/");
        String objectName = split[1];
        String[] split1 = objectName.split("\\.");
        String wei= split1[1];
// 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// 下载Object到本地文件，并保存到指定的本地路径中。如果指定的本地文件存在会覆盖，不存在则新建。
// 如果未指定本地路径，则下载后的文件默认保存到示例程序所属项目对应本地路径中。
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //组合filename
        if(wei.equals("mp4")){
            ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("D:\\下载图片/"+ uuid+".mp4"));
        } else  if(wei.equals("png")){
            ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("D:\\下载图片/"+ uuid+".png"));
        }else if(wei.equals("jpeg")){
            ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("D:\\下载图片/"+ uuid+".jpeg"));
        }else if(wei.equals("jpg")){
            ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File("D:\\下载图片/"+ uuid+".jpg"));
        }
// 关闭OSSClient。
        ossClient.shutdown();
        System.out.println("下载成功");
    }

}
